package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysGroupMapper;
import com.ruoyi.system.domain.SysGroup;
import com.ruoyi.system.service.ISysGroupService;

/**
 * 部门Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class SysGroupServiceImpl implements ISysGroupService
{
    @Autowired
    private SysGroupMapper sysGroupMapper;

    /**
     * 查询部门
     *
     * @param id 部门主键
     * @return 部门
     */
    @Override
    public SysGroup selectSysGroupById(Long id)
    {
        return sysGroupMapper.selectSysGroupById(id);
    }

    /**
     * 查询部门列表
     *
     * @param sysGroup 部门
     * @return 部门
     */
    @Override
    public List<SysGroup> selectSysGroupList(SysGroup sysGroup)
    {
        String groupArr = SecurityUtils.getLoginUser().getUser().getGroupArr();
        List<String> groupArrList = Arrays.asList(groupArr.split(","));
        List<SysGroup> sysGroups = sysGroupMapper.selectSysGroupList(sysGroup);
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysGroups= sysGroups.stream()
                    .filter(r -> {
                        return groupArrList.contains(r);
                    })
                    .collect(Collectors.toList()); //
        }
        return sysGroups;
    }

    /**
     * 新增部门
     *
     * @param sysGroup 部门
     * @return 结果
     */
    @Override
    public int insertSysGroup(SysGroup sysGroup)
    {
        sysGroup.setCreateTime(DateUtils.getNowDate());
        return sysGroupMapper.insertSysGroup(sysGroup);
    }

    /**
     * 修改部门
     *
     * @param sysGroup 部门
     * @return 结果
     */
    @Override
    public int updateSysGroup(SysGroup sysGroup)
    {
        sysGroup.setUpdateTime(DateUtils.getNowDate());
        return sysGroupMapper.updateSysGroup(sysGroup);
    }

    /**
     * 批量删除部门
     *
     * @param ids 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteSysGroupByIds(Long[] ids)
    {
        return sysGroupMapper.deleteSysGroupByIds(ids);
    }

    /**
     * 删除部门信息
     *
     * @param id 部门主键
     * @return 结果
     */
    @Override
    public int deleteSysGroupById(Long id)
    {
        return sysGroupMapper.deleteSysGroupById(id);
    }

    @Override
    public List<SysGroup> selectSysGroupListByids(Long[] ids) {
        return sysGroupMapper.selectSysGroupListByids(ids);
    }
}
